package com.wanxi.dao.impl;

import com.wanxi.dao.CuisineDao;
import com.wanxi.model.CuisineModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CuisineDaoImpl implements CuisineDao {
    @Override
    public int add(CuisineModel cuisineModel) {
        String sql = "insert into cuisine (name) values('" + cuisineModel.getName() + "')";
        return JDBC.update(sql);
    }

    @Override
    public int update(CuisineModel cuisineModel) {
        String sql = "update cuisine set name='" + cuisineModel.getName() + "' where id=" + cuisineModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int delete(CuisineModel cuisineModel) {
        String sql = "delete from cuisine where id=" + cuisineModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public CuisineModel findById(CuisineModel cuisineModel) {
        String sql = "select * from cuisine where id=" + cuisineModel.getId();
        List<CuisineModel> cuisineModelList = getData(sql);
        if (cuisineModelList.size() > 0) {
            return cuisineModelList.get(0);
        }
        return null;
//        ResultSet query = JDBC.query(sql);
//        CuisineModel model = null;
//        try {
//            while (query.next()) {
////                model = new CuisineModel();
////                model.setId(query.getInt("id"));
////                model.setName(query.getString("name"));
//                model = CuisineModel.builder()
//                        .id(query.getInt("id"))
//                        .name(query.getString("name"))
//                        .createTime(query.getString("create_time"))
////                        .updateTime(query.getString("update_time"))
//                        .enable(query.getInt("enable"))
//                        .build();
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return model;
    }

    @Override
    public List<CuisineModel> findAll(CuisineModel cuisineModel) {
        String sql = "select * from cuisine where 1=1 ";
        sql += getSql(cuisineModel);
        sql += " limit " + (cuisineModel.getPage() + -1) * cuisineModel.getLimit() + "," + cuisineModel.getLimit() + " ";
        return getData(sql);
    }

    private List<CuisineModel> getData(String sql) {
        ResultSet query = JDBC.query(sql);
        List<CuisineModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                CuisineModel model = CuisineModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .createTime(query.getString("create_time"))
//                        .updateTime(query.getString("update_time"))
                        .enable(query.getInt("enable"))
                        .build();
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int count(CuisineModel cuisineModel) {
        String sql = "select count(*) count from cuisine where 1=1 ";
        sql += getSql(cuisineModel);
        return JDBC.count(sql);
    }

    private String getSql(CuisineModel cuisineModel) {
        String sql = "";
        if (!"".equals(cuisineModel.getName())) {
            sql += " and  name like '%" + cuisineModel.getName() + "%'";
        }
        if (cuisineModel.getEnable() != -1) {
            sql += " and enable=" + cuisineModel.getEnable();
        }
        return sql;
    }
}
